package com.slt.concurrency.mashibing2019.juc.c_005_AtomicXXX.exercise;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class AtomicIntteger_API {
    private AtomicInteger count = new AtomicInteger(0);

    private void plus(){
        for (int i = 0; i < 100; i++) {
            count.incrementAndGet();
        }
    }

    public static void main(String[] args) {
        AtomicIntteger_API a = new AtomicIntteger_API();
        List<Thread> threadList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            threadList.add(new Thread(a::plus));
        }
        threadList.forEach(Thread::start);
        //把这100个线程 全部join住，否则可能线程还没有运行完毕，main线程就输出了 count值
        threadList.forEach(thread -> {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        System.out.println(a.count);
    }
}
